package homework04;

import java.time.LocalDateTime;
import java.util.Scanner;

public class UserSystem {
    private User[] users = new User[100];
    private int usercount = 0;

    public void register(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入注册用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入注册密码:");
        String userpassword = scanner.nextLine();

        for (int i = 0; i < usercount; i++) {
            if (users[i].getName().equals(username)) {
                System.out.println("用户名已存在,注册失败!");
                return;
            }
        }
        users[usercount++] = new User(username, userpassword);
        System.out.println("注册成功");
    }


    public void login(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入登录用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入登录密码:");
        String userpassword = scanner.nextLine();

        for (int i = 0; i < usercount; i++) {
            if (users[i].getName().equals(username) && users[i].getPassword().equals(userpassword)) {
                users[i].setLoginTime(LocalDateTime.now());
                System.out.println("登录成功!");
                return;
            }
        }
        System.out.println("用户名或密码错误，登录失败！");
    }
}
